Yacon.component.TSqlGridBody = function(element){
	this.element = element;	
	
	if(this.element.width){
		this.element.width = this.element.width - 2;
	}
	
	this.isXScoll = false;
	
};

Yacon.component.TSqlGridBody.prototype = {
	
	getHtml: function(){
		
		var html = "";
		
		html += "<div class='panel-body row-ht-auto col' style=''>";	
		
		//查询部分处理
		html += this.getFormHtml();
				
		html += "<div class='table-responsive row-ht-auto col' style='width:100%;overflow-x: auto;overflow-y:hidden; border:1px solid #DDDDDD'>";
		
		html += "</div>";

		//底部页码处理 -- 有查询的情况底部加边距 - BUG
		var formEle = this.element.grid.form;
		
		if(!Yacon.isEmpty(formEle)){
			html += "<div class='lily-table-foot row-ht-fix' style='padding-bottom:15px;'>";	
		}else{
			html += "<div class='lily-table-foot row-ht-fix' >";	
		}
		
		//Foot部分添加
		if(this.parent.foot){		
			html += this.parent.foot.getHtml();			
		}
		
		html += "</div>";
		
		html += "</div>";
		
		return html;
		
	},
	
	/* 标题部分HTML */
	getTitleHtml: function(){
		
		var html = "";
		
		html += "<div class='row-ht-fix table-title' style='width: "+this.element.width+"px;'>";		
		html += "<table class='table clear-margin-bottom fix table-bordered' style='table-layout:fixed;word-break:break-all;' >";		
		html += "<thead>";		
		html += "<tr>";
		
		var choice = this.element.grid.choice;
		if(choice == "flat"){
			
		}else{
			
			if($("body").hasClass("mid")){
	            html += "<th style='width:25px;'>";        
	            html += "<label class='checkbox-inline'>";
	            html += "<input class='check-all-field lily-check' style='position:static;' type='checkbox' id='inlineCheckbox1' value='option1'>&nbsp;</input>";
			}else{
				html += "<th style='width:20px;'>";		
				html += "<label class='checkbox-inline' style='padding-left: 21px;'>";
				html += "<input class='check-all-field lily-check' style='position:static;width:10px;' type='checkbox' id='inlineCheckbox1' value='option1'>&nbsp;</input>";
			}
	
			html += "</label>";		
			html += "</th>";
		}
		
		var alls = this.element.grid.fields;
		var all_size = alls.length;
		for(var i=0;i<all_size;i++){
			html += this.getFieldHtml(alls[i]);
		}
		
		html += "</tr>";
		html += "</thead>";
		html += "</table>";	
		html += "</div>";
		
		return html;
	},
	
	/* 获取字段HTML信息 */
	getFieldHtml: function(field){
		
		var html = "";
		
		var style = "";
		if(!field.isFinal){
			style += "width:"+field.width+"px;";
		}
			
		if(!field.isShow){
			style += "display:none;";
		}
		
		style += "word-wrap:break-word;";
		
		style += "text-align:"+field.titleAlign+";";
		
		html += "<th style='"+style+"' field_name='"+field.name+"' field_text='"+field.text+"' field_val='"+field.value+"' ";
		
		html += " field_exp='"+field.dropexp+"' field_typ='"+field.type+"' ";
		
		html += ">";
		
		html += field.text;
		
		html += "</th>";
		
		return html;
	},
	
	/* 获取体部分HTML */
	getBodyHtml: function(){
		
		var html = "";
		
		html += "<div class='row-ht-auto table-body' style='width: "+this.element.width+"px;overflow-y:auto;' ";
		
		if(this.element.grid.menu){
			if(this.element.grid.menu.type == "right"){
				html += " data-toggle='context' data-target='#"+this.element.id+"-menu' ";
				html += " id='"+this.element.id+"-context' ";
			}
		}
		
		html += ">";
		
		html += "<table class='table clear-margin-bottom table-bordered' style='table-layout:fixed;word-break:break-all;' >";
		html += "<tbody>";
		
		html += this.getDataHtml();
		
		html += "</tbody>";
		html += "</table>";
		
		if(this.isXScoll){
			html += "<div style='height:15px'></div>";
		}
		
		html += "</div>";
		
		return html;
		
	},
	
	/* 数据部分HTML */
	getDataHtml: function(){
		var html = "";
		
		var rowDatas = this.element.data;		
		var rowSize = rowDatas.length;
		
		for(var i=0;i<rowSize;i++){
			
			html += "<tr>";
			
			var choice = this.element.grid.choice;
			if(choice == "flat"){
				
			}else{
				
				if($("body").hasClass("mid")){
	                html += "<td style='width:25px;word-wrap:break-word;'>";        
	                html += "<label class='checkbox-inline'>";
	                html += "<input type='checkbox' style='position:static;' class='check-field lily-check' id='inlineCheckbox1' value='option1'>&nbsp;</input>";
				}else{
					html += "<td style='width:20px;word-wrap:break-word;'>";		
					html += "<label class='checkbox-inline' style='padding-left: 21px;'>";
					html += "<input type='checkbox' style='position:static;width:10px;' class='check-field lily-check' id='inlineCheckbox1' value='option1'>&nbsp;</input>";
				}
				
				html += "</label>";		
				html += "</td>";
			}
			
			var colDatas = rowDatas[i];
			var colSize = colDatas.length;
			
			for(var j=0;j<colSize;j++){
				
				if(i == 0){
					
					html += this.getDataFieldHtml(colDatas[j], this.element.grid.fields[j],true);
										
				}
				else{
				    html += this.getDataFieldHtml(colDatas[j], this.element.grid.fields[j],false);
				}
				
			}
			
			html += "</tr>";
			
		}
			
		return html;
	},
	
	/* 获取数据HTML部分 */
	getDataFieldHtml: function(data,field,isWidth){
		
		var html = "";
		
		var style = "";
				
		if(!field.isShow){
			style += "display:none;";
		}
		
		style += "word-wrap:break-word;";
		
		style += "text-align:"+field.dataAlign+";";
		
		if(isWidth){			
			if(!field.isFinal){
				style += "width:"+field.width+"px;";
			}
		}
			
		if(style == ""){
			html += "<td ";
		}else{
			html += "<td style='"+style+"' ";
		}
		
		html += " field_name='"+data.name+"' field_text='"+data.text+"' field_val='"+data.value+"' ";
		
		html += " field_typ='"+field.type+"' field_exp='"+field.dropexp+"' ";
		
		html += ">";
		
		html += data.text;
		
		html += "</td>";
			
		return html;
		
	},
	
	getFormHtml: function(){
		
		var html = "";
		
		var formEle = this.element.grid.form;
		
		if(!Yacon.isEmpty(formEle)){
			
			html += "<div class='table-form row-ht-fix'>";
			
			var findform = new Yacon.widget.TForm({view:formEle,page:this.parent.page,parent:this.parent});		
			this.parent.page.addComponet(findform);		
			this.parent.findform = findform;
			html += findform.getHtml();
			
			html += "</div>";
		}
		
		return html;
		
	},
	
	/* 自动宽度调整 */
	autoWidth: function(){
		
		this.isCheck = false;
		if(this.parent.view.grid.choice == "check"){
			this.isCheck = true;
		}
			
		var winWidth = this.element.width;
		
		winWidth = this.getNumber(winWidth);
		
		var show_fields = new Array();
		
		/* 处理需要展示信息 */
		var alls = this.element.grid.fields;
		var all_size = alls.length;
		for(var i=0;i<all_size;i++){
			if(alls[i].isShow){
				show_fields.add(alls[i]);
			}
		}
		
		/*二次遍历，预处理信息*/
		var show_size = show_fields.length;
		var fixLength = 0;
		var totalLength = 0;
		var autoSize = 0;
		for(var i=0;i<show_size;i++){
						
			show_fields[i].width = this.getNumber(show_fields[i].width);
					
			var active_width = show_fields[i].width;
			
			totalLength += active_width;
			
			if(active_width == 120){
				autoSize ++;
			}
			else{
				fixLength += active_width;
			}

			if(i == (show_size-1)){
				show_fields[i].isFinal = true;
			}else{
				show_fields[i].isFinal = false;
			}
				
		}
		
		var totalAllLength = totalLength;
		if(this.isCheck){
			totalAllLength += 25;
			fixLength += 25;
		}
		
		/* 三次遍历，最终结果 */
		if(totalAllLength >= winWidth){
			this.element.width = totalAllLength;
			this.isXScoll = true;
		}else{
			
			var autoLength = (winWidth - fixLength) / autoSize;
			for(var i=0;i<show_size;i++){
				
				var active_width = show_fields[i].width;
				if(active_width == 120){
					show_fields[i].width = autoLength;
				}
				
			}
			
		}
		
		
	},
	
	/* 根据宽度值 - 获取数值 */
	getNumber: function(widthStr){
		
		widthStr = widthStr + "";
		
		widthStr = widthStr.trim();
		
		if(widthStr.endWith("px")){
			return parseFloat(widthStr.substring(0,widthStr.length-2));
		}
		
		else if(widthStr.endWith("%")){			
			var num = parseFloat(widthStr.substring(0,widthStr.length-1)) / 100;		
			return Yacon.getWidthNumber(this.element.width) * num;
		}
		
		else{			
			return parseFloat(widthStr);		
		}
		
	},
	
	refresh: function(){
			
		delete this.element;
		
		this.element = this.parent.view;
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		
		var width = $(search+" .panel-body .table-responsive").width();
		
		this.element.width = width;
		
		this.autoWidth();
		
		var html = this.getTitleHtml() + this.getBodyHtml();
		
		$(search+" .panel-body .table-responsive").empty();
		$(search+" .panel-body .table-responsive").append(html);
		
		/* 重新布局 */
		this.parent.page.layoutOuter(search+" .panel-body");
		
	},
	
	reset: function(){
		
		var search = this.parent.page.element.search + " div[lily-id='"+this.parent.elementID+"'][lily-type='"+this.parent.elementType+"']";
		$(search+" .panel-body .table-responsive tbody").empty();
		
	}
		
};